using System.Collections.Generic;
using System.Collections;
namespace Gl.SettingsManager
{
	public class SettingsCollection : IEnumerable<Setting>
	{
		private string _typeName;
		private string _contextName;

		public string ContextName
		{
			get { return _contextName; }
			set { _contextName = value; }
		}

		public string TypeName
		{
			get { return _typeName; }
			set { _typeName = value; }
		}

		public SettingsCollection(string typeName, string contextName)
		{
			_typeName = typeName;
			_contextName = contextName;
		}

		private Dictionary<string, Setting> _settings = new Dictionary<string, Setting>();

		public string this[string settingName]
		{
			get
			{
				if (_settings.ContainsKey(settingName))
					return _settings[settingName].Value;

				return null;
			}
			set
			{
				if (_settings.ContainsKey(settingName))
					_settings[settingName].Value = value;
				else
					_settings.Add(settingName, new Setting(settingName, value));
			}
		}

		public void Remove(string settingName)
		{
			if (_settings.ContainsKey(settingName))
				_settings.Remove(settingName);
		}

		public void Add(string name, string value)
		{
			if (_settings.ContainsKey(name))
				throw new SettingsException(Exceptions.SettingAlreadyExists, name);

			_settings.Add(name, new Setting(name, value));
		}

		public void Copy(SettingsCollection settings)
		{
			_settings.Clear();

			_contextName = settings.ContextName;
			_typeName = settings.TypeName;

			foreach (Setting set in settings)
			{
				_settings.Add(set.Name, set);
			}
		}

		#region IEnumerable<Setting> Members

		public IEnumerator<Setting> GetEnumerator()
		{
			foreach (Setting setting in _settings.Values)
				yield return setting;
		}

		#endregion

		#region IEnumerable Members

		IEnumerator IEnumerable.GetEnumerator()
		{
			return GetEnumerator();
		}

		#endregion
	}
}
